package com.zahb.qadx.ui.activity;

import android.os.Build;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.util.WebViewHelper;

import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by： huwei.
 * Data： 2020-10-26.
 * Time： 17:06.
 * Desc：webview
 */
public class WebViewActivity extends BaseActivity {
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.webView)
    WebView webView;

    @Override
    protected int getLayout() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);
        WebSettings settings = webView.getSettings();
        // 支持方大缩小
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        // 设置编码
        settings.setDefaultTextEncodingName("utf-8");
        //隐藏缩放控件
        settings.setDisplayZoomControls(false);
        //自适应屏幕
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        settings.setDatabaseEnabled(true);
        // 支持js
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setUseWideViewPort(true);

        //提高渲染优先级
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
//        settings.setAppCacheMaxSize(1024 * 1024 * 8);
//        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
//        settings.setAppCachePath(appCachePath);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, final int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (progressBar != null) {
                    if (newProgress == 100) {
                        progressBar.setVisibility(View.GONE);
                    } else {
                        if (View.GONE == progressBar.getVisibility()) {
                            progressBar.setVisibility(View.VISIBLE);
                        }
                        progressBar.setProgress(newProgress);
                    }
                }

            }
        });

        /*webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Log.e("webView request url", request.getUrl().toString());
                    webView.loadUrl(request.getUrl().toString());
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("webView url", url);
                webView.loadUrl(url);
                return true;
            }
        });*/

        String type = getIntent().getStringExtra("type");
        String token = URLEncoder.encode(BaseApplication.getContext().getToken());
        if ("1".equals(type)) {
            int id = getIntent().getIntExtra("id", 0);
            webView.loadUrl(WebViewHelper.getBaseUrl() + "news/newsDetail" + "?id=" + id + "&token=" + token + "&fromType=" + 1);
            Log.e("lianji",WebViewHelper.getBaseUrl() + "news/newsDetail" + "?id=" + id + "&token=" + token + "&fromType=" + 1);
        }
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.news_detail;
    }
}
